{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PyNE Tutorial\n",
    "=============\n",
    "\n",
    "Welcome to the PyNE tutorial! \n",
    "\n",
    "<center>\n",
    "    <img alt=\"PyNE\" width=\"50%\" src=\"https://raw.github.com/pyne/pyne/develop/img/pyne_icon_big.png\" style=\"max-width: 30rem;\" />\n",
    "</center>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Before you begin\n",
    "* Hopefully you've installed the virtual machine if necessary. \n",
    "    - If not, please go to https://pyne.io/install/vb.html to install a virtual machine.\n",
    "    - If so, open it now and open Accessories -> LXTerminal, navigate to ~/opt/pyne"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Is PyNE Installed?\n",
    "\n",
    "You can verify that PyNE is successfully installed by running the following: \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "235.04392819\n"
     ]
    }
   ],
   "source": [
    "from pyne import data\n",
    "print(data.atomic_mass('U235'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Open the tutorial\n",
    "\n",
    "You can start the tutorial by navigating to `~/opt/pyne/tutorial` and entering\n",
    "\n",
    "```bash\n",
    "$ jupyter notebook --matplotlib=inline\n",
    "```\n",
    "\n",
    "This should open the tutorial in a web browser. We'll start with `00-intro`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What is PyNE?\n",
    "\n",
    "As our tagline says, PyNE is an open-source nuclear engineering toolkit.  \n",
    "\n",
    "* It serves as a library of composable tools for building nuclear science and engineering applications.\n",
    "* It is permissively licensed under the 2-clause BSD license.\n",
    "* It supports both a C++ and a Python API.\n",
    "* While the name suggests a focus on Python, most of the code base is actually written in C++.\n",
    "* Daily usage and interactions with PyNE primarily occur in Python.\n",
    "* The current stable release of PyNE is version 0.7.7.\n",
    "* PyNE was established as an organization in April 2011, but certain core parts of PyNE have existed since 2007."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What are the goals of PyNE?\n",
    "\n",
    "To help nuclear engineers:\n",
    "\n",
    "* be more productive,\n",
    "* have the *best* solvers,\n",
    "* have a beautiful API,\n",
    "* write really great code,\n",
    "* and teach the next generation.\n",
    "\n",
    "And of course, to perform neat tricks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from pyne import mesh, material \n",
    "from pyne.xs.channels import sigma_t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Known error: This notebook requires [yt](https://yt-project.org/) to visualize the results. Yt needs to be updated to work properly first. Updates are currently being made to yt's frontends to make this PyNE integration work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img style=\"max-width:100%;max-height:100%;\" src=\"\"><br>"
      ],
      "text/plain": [
       "<yt.visualization.plot_window.AxisAlignedSlicePlot at 0x7f9fcfded870>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "U Content of Mesh: 1732.1034518863273 grams\n"
     ]
    }
   ],
   "source": [
    "# import a few things necessary for plotting in the notebook\n",
    "from yt.frontends.moab.api import PyneMoabHex8Dataset\n",
    "from yt import SlicePlot\n",
    "import logging\n",
    "\n",
    "# Disable all logging outputs\n",
    "logging.disable(logging.CRITICAL)\n",
    "\n",
    "# Create reactor mesh\n",
    "m = mesh.Mesh(structured_coords=[np.linspace(0.0, 1.0, 101), np.linspace(0.0, 1.0, 101), [0.0, 1.0]], structured=True)\n",
    "fuel = material.from_atom_frac({'U235': 0.045, 'U238': 0.955, 'O16': 2.0}, mass=1.0, density=10.7)\n",
    "cool = material.from_atom_frac({'H1': 2.0, 'O16': 1.0}, mass=1.0, density=1.0)\n",
    "for i, mat, ve in m:\n",
    "    coord = m.mesh.get_coords(ve)\n",
    "    m.mats[i] = fuel if (coord[0]**2 + coord[1]**2) <= 0.5**2 else cool\n",
    "\n",
    "# Create a total cross section tag on the mesh\n",
    "m.sigma_t = mesh.ComputedTag(lambda mesh, i: sigma_t(mesh.mats[i], group_struct=[10.0, 1e-6], phi_g=[1.0])[0])\n",
    "\n",
    "# Create a uranium mass tag on the mesh\n",
    "m.u_mass = mesh.ComputedTag(lambda mesh, i: max(mesh.mats[i]['U':'Np'].mass, 0.0))\n",
    "\n",
    "# Plot the total cross section\n",
    "slc = SlicePlot(PyneMoabHex8Dataset(m), 'z', 'sigma_t', origin='native')\n",
    "slc.annotate_title(\"Total cross section\")\n",
    "slc.set_cmap('sigma_t', \"RdBu_r\")\n",
    "slc.display()\n",
    "\n",
    "# Print the total mass of uranium in the reactor mesh\n",
    "print(\"U Content of Mesh:\", sum(m.u_mass[:]), \"grams\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What could you do with PyNE?\n",
    "\n",
    "As a **user**, using PyNE for your work or research eliminates the need for you to create and upkeep similar functionality in your own software. By using PyNE, you can save time and effort in developing and maintaining those features.\n",
    "\n",
    "As a **developer**, it is beneficial to be self-interested. Contributing to PyNE in ways that align with your work will not only support your own endeavors but also cater to the needs of other individuals. If there is a feature you require that is currently absent in PyNE, chances are that others would also benefit from its inclusion. Contributing to PyNE in this manner ensures that your future self, as well as other individuals in the future, can reap the rewards of enhanced functionality."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What goes into PyNE?\n",
    "\n",
    "Anything that is not export controllable, proprietary, or under HIPAA restrictions!  (If you have questions, ask.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contact PyNE\n",
    "\n",
    "**Website:** https://pyne.io\n",
    "\n",
    "**User's Mailing List:** pyne-users@googlegroups.com\n",
    "\n",
    "**Developer's List:** pyne-dev@googlegroups.com"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
